package com.itheima.代理方式实现Dao_CRUD.service;

import com.github.pagehelper.PageHelper;
import com.itheima.代理方式实现Dao_CRUD.domain.Student;
import com.itheima.代理方式实现Dao_CRUD.mapper.StudentMapper;
import com.itheima.代理方式实现Dao_CRUD.utils.MyBatisUtils;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;

import java.util.List;

public class ServiceTest {

    /*===========代理方式实现Dao_CRUD=====================================================*/
    //查询全部
    @Test
    public void selectAll() {
        //1获取SqlSession对象
        SqlSession sqlSession = MyBatisUtils.getSqlSession();
        //2.获取StudentMapper的接口实现类对象
        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
        //3.使用mapper调用其方法
        List<Student> list = mapper.selectAll();
        sqlSession.close();
        //4.处理结果集
        for (Student stu : list) {
            System.out.println(stu);
        }
    }

    //添加信息
    @Test
    public void save() {
        //1获取SqlSession对象
        SqlSession sqlSession = MyBatisUtils.getSqlSession();
        //2.获取StudentMapper的接口实现类对象
        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
        //3.使用mapper调用其方法
        Student stu = new Student();
        stu.setName("萧蔷");
        stu.setAge(21);
        stu.setGender("女");
        int save = mapper.save(stu);
        sqlSession.commit();
        sqlSession.close();
        //4.处理结果集
        System.out.println(save);
    }

    //删除信息
    @Test
    public void delete() {
        //1获取SqlSession对象
        SqlSession sqlSession = MyBatisUtils.getSqlSession();
        //2.获取StudentMapper的接口实现类对象
        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
        //3.使用mapper调用其方法
        int delete = mapper.delete(22);
        sqlSession.commit();
        sqlSession.close();
        //4.处理结果集
        System.out.println(delete);
    }

    //修改信息
    @Test
    public void update() {
        //1获取SqlSession对象
        SqlSession sqlSession = MyBatisUtils.getSqlSession();
        //2.获取StudentMapper的接口实现类对象
        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
        //3.使用mapper调用其方法
        Student stu = new Student();
        stu.setName("萧峰");
        stu.setAge(24);
        stu.setGender("男");
        stu.setId(1);
        int update = mapper.update(stu);
        sqlSession.commit();
        sqlSession.close();
        //4.处理结果集
        System.out.println(update);
    }

    /*=======================动态sql====================================================*/

    //根据学生的 id 或者 姓名 或者 年龄 或者 性别 查询 --- 因为不知道用户具体会输入具体几个参数,所以产生动态sql技术
    //@Param注解用来解决多参数查询时的情况
    //模糊查询
    @Test
    public void selectByIdOrNameOrAgeOrGender() {
        //1获取SqlSession对象
        SqlSession sqlSession = MyBatisUtils.getSqlSession();
        //2.获取StudentMapper的接口实现类对象
        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
        //3.使用mapper调用其方法
        List<Student> list = mapper.selectByIdOrNameOrAgeOrGender(0, "萧蔷", 0, null);
        sqlSession.close();
        //4.处理结果集
        for (Student stu : list) {
            System.out.println(stu);
        }
    }

    /*=======================分页插件查询====================================================*/

    //分页查询全部
    @Test
    public void selectAllPlugins() {
        //1获取SqlSession对象
        SqlSession sqlSession = MyBatisUtils.getSqlSession();
        //2.获取StudentMapper的接口实现类对象
        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
        //3.使用mapper调用其方法
        PageHelper.startPage(2,2);
        List<Student> list = mapper.selectAll();
        sqlSession.close();
        //4.处理结果集
        for (Student stu : list) {
            System.out.println(stu);
        }
    }

}
